
package global.com.loansapplication.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.Date;
import java.util.List;

import global.com.loansapplication.R;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.comm.Constants;
import global.com.loansapplication.ui.actviity.user.CustomAct;
import global.com.loansapplication.ui.widget.flowlayout.FlowLayout;
import global.com.loansapplication.ui.widget.flowlayout.TagAdapter;
import global.com.loansapplication.ui.widget.flowlayout.TagFlowLayout;
import global.com.loansapplication.util.StringUtils;
import global.com.loansapplication.util.SysUtils;

/**
 * chenjian
 */
public class ClueAdapter extends SuperBaseAdapter<ClueInfo> {
    Context mContext;

    public ClueAdapter(Context context, List<ClueInfo> data) {
        super(context, data);
        mContext = context;


    }

    @Override
    protected void convert(BaseViewHolder holder, final ClueInfo item, final int position) {
        String info = (StringUtils.isEmpty(item.getCity()) ? "" : item.getCity()) + (StringUtils.isEmpty(item.getZhiye()) ? "" : " |" + item.getZhiye()) + (item.getShouru() == null ? "" : "  |月收入" + item.getShouru());
        holder.setText(R.id.item_collection_name, item.getName()).setText(R.id.item_collection_phone, item.getMobile()).
                setText(R.id.item_collection_money, item.getPrice() + "").setText(R.id.item_collection_city, info).
                setText(R.id.item_collection_dai_time, "贷款期限 " + item.getMonth() + "个月")
                .setText(R.id.item_collection_time, "更新时间：" + SysUtils.getDatePoorNew(new Date(), item.getUpdateTime()))
                .setText(R.id.item_source, "来源：" + item.getSource());

        TextView state = holder.getView(R.id.item_collection_state);
        if (item.getSoldQuantity() == item.getUseCount()) {
            state.setText("已抢完");
            state.setBackgroundResource(R.drawable.bg_shape_gray);
        } else if (item.getSoldQuantity() == 0) {
            state.setText("马上抢");
            state.setBackgroundResource(R.drawable.bg_shape_red);
        } else if (item.getSoldQuantity() < item.getUseCount() && item.getSoldQuantity() > 0) {
            state.setText("已抢" + item.getSoldQuantity() + "/" + item.getUseCount());
            state.setBackgroundResource(R.drawable.bg_shape_green);
        }
        String[] lable = item.getLabel().split("；");
        TagFlowLayout FlowTagLayout = holder.getView(R.id.item_collection_tip);

        if (StringUtils.isEmpty(item.getLabel())) {
            FlowTagLayout.setVisibility(View.GONE);
        }

        FlowTagLayout.setAdapter(new TagAdapter<String>(lable) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.tag_item, null);
                view.setText(s);
                return view;
            }
        });

        holder.getView(R.id.item_collection_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, CustomAct.class);
                intent.putExtra("position", position + "");
                intent.putExtra(Constants.KEYSTRING, item.getStudentCode());
                mContext.startActivity(intent);
            }
        });
        state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CustomAct.class);
                intent.putExtra("position", position + "");
                intent.putExtra(Constants.KEYSTRING, item.getStudentCode());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    protected int getItemViewLayoutId(int position, ClueInfo item) {
        return R.layout.item_clue;
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

    }


}